local Luashenwaishen = fk.CreateSkill({
  name = "Luashenwaishen",
  tags = {Skill.Compulsory}
})

Fk:loadTranslationTable{
  ["Luashenwaishen"] = "身外身",
  [":Luashenwaishen"] = "锁定技，出牌阶段，你每使用一张牌，获得一枚“分身”标记（至多3枚）。当你使用普通锦囊牌或【杀】时，令此牌额外结算X次（X为“分身”数）。摸牌阶段摸牌数+X，手牌上限+X（X为“分身”数）。杀基础伤害+X。攻击距离+X。攻击目标+X。",
  ["@shen"] = "分身",
}

-- 使用牌获得分身标记
Luashenwaishen:addEffect(fk.PreCardUse, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(Luashenwaishen.name) and player.phase == Player.Play and player:getMark("@shen") < 3
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    
    -- 获取分身标记数量，不超过3枚
    local mark = math.min(player:getMark("@shen") + 1, 3)
    room:setPlayerMark(player, "@shen", mark)
  end,
})

-- 普通锦囊牌或杀额外结算
Luashenwaishen:addEffect(fk.CardUsing, {
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(Luashenwaishen.name) then return false end
    
    local mark = player:getMark("@shen")
    if mark <= 0 then return false end
    
    local card = data.card
    return (card:isCommonTrick() or card.trueName == "slash")
  end,
  on_use = function(self, event, target, player, data)
    local mark = player:getMark("@shen")
    data.additionalEffect = (data.additionalEffect or 0) + mark
    data.additionalDamage = (data.additionalDamage or 0) + mark
  end,
})

-- 摸牌阶段摸牌数+X
Luashenwaishen:addEffect(fk.DrawNCards, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(Luashenwaishen.name) and player:getMark("@shen") > 0
  end,
  on_use = function(self, event, target, player, data)
    data.n = data.n + player:getMark("@shen")
  end,
})

-- 手牌上限+X
Luashenwaishen:addEffect("maxcards",{
  correct_func = function(self, player)
    if player:hasSkill(Luashenwaishen.name) then
      return player:getMark("@shen")
    end
  end,
}) 

-- 杀基础伤害+X，攻击距离+X，攻击目标+X
Luashenwaishen:addEffect("targetmod",{
    residue_func = function(self, player, skill, scope, card)
        if player:hasSkill("Luafanqie") then
            local mark = player:getMark("@shen")
            return mark
        end
    end,
    extra_target_func = function(self, player, skill, card)
        if player:hasSkill("Luafanqie")  then
            local mark = player:getMark("@shen")
            return mark
        end
    end,
})

return Luashenwaishen 